package com.yedean.mall.service.impl;

import com.yedean.mall.pojo.User;
import com.yedean.mall.service.UserService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
    
    //创建map集合
    private final Map<Integer,User> userMap=new HashMap<>();
    
    public UserServiceImpl(){
        this.userMap.put(1,new User("yda","male",22));
        this.userMap.put(2,new User("lll","female",22));
        this.userMap.put(3,new User("wx","male",28));
    }
    @Override
    public Mono<User> getUserById(int id) {
        return Mono.justOrEmpty(this.userMap.get(id));
    }

    @Override
    public Flux<User> getAllUser() {
        return Flux.fromIterable(userMap.values());
    }

    @Override
    public Mono<Void> saveUser(Mono<User> userMono) {
        return userMono.doOnNext(user -> {
            //向map集合里面
            int id=userMap.size()+1;
            userMap.put(id,user);
        }).thenEmpty(Mono.empty());
    }
}
